Repository navigation
Bounds-check host register writes in handle_dma - #12
Conversation
handle_dma() trusted the packet's count byte and every write address. libwallaby zero-pads its packets, so any count larger than the real number of writes applied (0, 0) pairs and cleared the 'J' start byte, after which libwallaby logs "DMA de-synchronized" on every transfer until the STM32 resets. Larger counts read past the 89 bytes the DMA fills and then into aTxBuffer itself, and addresses of 153 or more wrote past the end of aTxBuffer. Packets whose count exceeds the 42 pairs that fit before the trailing 'S' are now ignored, and writes to the start and version registers or past REG_ALL_COUNT are skipped. libwallaby sends 1-4 writes to addresses 3-152, so normal traffic is unaffected. Part of #11.
Dropping every write from a packet whose count byte exceeds 42 lost the real write whenever a count-byte bit flip pushed it above 42. That includes a flip of the top bit, which master happened to handle correctly because the count wrapped back to its real value. libwallaby zero-pads its packets and the address check already skips (0, 0) pairs, so clamping the count to 42 applies the real writes and nothing else. Also corrects the address-check comment: libwallaby checks only the start byte on every transfer, and nothing on the host reads the version registers. They're protected because the register map makes them read-only. Part of #11.
Clamping an oversized count to 42 applied 42 pairs even when the body was garbage too. On master a count of 128-169 wrapped and applied fewer. The real count is now recovered from where libwallaby's zero padding starts, and a packet with no padding is dropped. Single-bit flips of the count byte still apply exactly the real writes. Packets with a corrupt count or a skipped write used to be silent. They are now counted, mod 16, in the unused high nibble of REG_RW_MOT_DONE. The nibble is republished on every packet, so the main loop's read-modify-write of the done bits can't hold back a stale value. libkipr masks the done bits, so existing hosts are unaffected. Part of #11. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
I tested this on a Wombat and pushed one commit on top, Hardware results for this PR as submitted (
|
| Test | master | this PR |
|---|---|---|
| Your repro (count 2, no pairs) | 199/200 frames desynced | 0/200 |
Servo register round-trip (w8/w16/w32) |
pass | pass |
Every single-bit flip of the count byte on a w16 |
desync on 6 of 8 flips | real write applied, no desync |
| Writes to registers 0..2 | start byte overwritten, desync | ignored |
| 20,000-frame soak | 0 desync, 4796 frames/s | 0 desync, 4798 frames/s |
A host-compiled differential test of handle_dma() (master vs. this PR, aRxBuffer/aTxBuffer laid out as in the memory map) found no difference for any legitimate frame: all 65,536 single writes to 3..152, every w16/w32 start address, and 200,000 random frames of 1..42 valid pairs.
Two things the clamp made worse, fixed in 6896e00
-
A corrupt count over a non-zero body applied more writes than master. With a count of 128..169, master's
stop = 2 * num_regswraps inuint8_t, so it applies n−128 pairs. Clamping to 42 applies all 42. With random bodies, the clamp changed more registers than master in 96% of such frames (22.8 vs. 11.7 bytes on average). On hardware, a count of 128 over an unpadded body moved servo 0's register to 2000 where master left it at 1500.Now a count above 42 is treated as corrupt and the real count is recovered from where the zero padding starts. If there is no padding before
'S', the body is as suspect as the count and nothing is applied. Single-bit flips of the count byte on libwallaby frames still apply exactly the real writes. -
Corruption became silent. Master turned an inflated count into a permanent
DMA de-synchronized, which is bad but visible. With the clamp there was no signal at all, anddebug_printfis an empty stub.Packets with a corrupt count or a skipped write are now counted, mod 16, in bits 7:4 of
REG_RW_MOT_DONE. Those bits were unused: libkipr andwallaby_motor.cboth mask the low four. The one behaviour change is that a host write to register 60 can no longer set bits 7:4; nothing in libwallaby writes that register.The host side is a small libwallaby change that logs a rate-limited warning when the counter moves. I'll open that separately. Stock libkipr 1.2.4 ignores the nibble and works unchanged against this firmware.
Hardware results with 6896e00
- All of the tests above still pass.
- Count 128 over an unpadded body: servo 0 stays at 1500.
- Reject counter: moves by exactly 1 for each of three kinds of rejected frame, 0 drift over 100 clean frames.
- Regression check through libkipr, stock 1.2.4 and patched, motors and servos on ports 0 and 3:
motor,off,freeze,mav,mrp/mtpwith the done bit, servo enable and positions, digital output registers on ports 0..9, analog, digital and button reads. All pass, and the motors and servos were watched moving.- The done bit stays set across three injected rejected frames while the counter moves by 3.
- Not tested: the electrical level on a driven digital output, and buttons or analog sensors being changed by hand.
scripts/check-register-map.sh passes and the files keep their CRLF line endings. #13 merges onto this with no conflict and builds; I haven't flashed that combination.
Part of #11 (item 1).
What and why
handle_dma()trusted the count byte and every address in a host packet.(0, 0)pairs. That writes 0 to register 0, the'J'start byte, and from then on libwallaby logsDMA de-synchronizedon every transfer until the STM32 resets. A single bit flip that raises the count is enough.aTxBufferitself, treating sensor bytes as(address, value)pairs.aTxBuffer, into the analog filter state and other globals.Changes
Firmware/src/wallaby_dma.c:'S'. Clamping rather than dropping the packet keeps the real writes when only the count byte is corrupted: libwallaby zero-pads its packets, and the address check skips those(0, 0)pairs.REG_RW_DIG_IN_H) and to addresses at or aboveREG_ALL_COUNT. The check runs before any side effects (done-bit clearing,adc_dirty,dig_dirty).TODO: register/value checks before assignmentcomment.AGENTS.md: thehandle_dma()description now mentions the new checks.The register map is unchanged. libwallaby sends 1–4 writes per packet to addresses 3–152 (
platform.cppalready rejects ≥ 153), so normal traffic is unaffected.Verification
wallaby_dma.cstill compiles with no warnings; the build's 8 warnings are the same newlib syscall-stub linker warnings as onmaster.cmp r1, #42; movcs r1, #42), and an unsigned range check covers addresses 3–152.aRxBufferdirectly followed byaTxBufferas in the memory map:masterclears'J', except the 128-bit flip, which wraps back to the real count. With this PR, the real write is applied and nothing else is written.analog(0). Expected:DMA de-synchronizedon every call untilwallaby_reset_coproc.wombat.bin(CI artifact) and repeat. Expected: no errors.motor,mav,off), servos, and digital and analog I/O.